
import { ServerConfig } from "../config/ServerConfig";
import { SocketClientReqManager } from "../engine/socketclient/SocketClientReqManager";
import { SocketConnectionManager } from "../engine/socketserver/socket/SocketConnectionManager";
import { HttpUtil } from "../engine/utils/HttpUtil";
import { Logger } from "../engine/utils/Logger";
import { GameHttpHelper } from "../game/helper/GameHttpHelper";
import { RoomManager } from "./RoomManager";

export class ServerManager {

    public static instance: ServerManager = new ServerManager();

    // public initServerSockets() {
    //     let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
    //     for (let i = 0; i < hall_server_list.length; i++) {
    //         let server: ServerConfig = hall_server_list[i]
    //         if (server.id != ServerConfig.nowServerConfig.id) {
    //             SocketClientReqManager.instance.req_server_login(server)
    //         }
    //     }
    // }

    public async set_allserver_status(status: number) {
        // let all_server_list: Array<ServerConfig> = ServerConfig.getAllSocketServer();
        // for (let i = 0; i < all_server_list.length; i++) {
        //     let server: ServerConfig = all_server_list[i]
        //     if (server.id == ServerConfig.nowServerConfig.id) {
        //         server.status = status
        //     } else {
        //         SocketClientReqManager.instance.req_server_set_server_status(status, server)
        //     }
        // }
    }

    private async getServerListOnlineNum(serverList: Array<ServerConfig>) {
        let onlineNum: number = 0
        Logger.log("get_all_server_online_num=serverList=", serverList)
        for (let i = 0; i < serverList.length; i++) {
            let server: ServerConfig = serverList[i]
            let response = await GameHttpHelper.get_online_num(server.getInternalUrl() + "/online")
            Logger.log("get_all_server_online_num==", response)
            onlineNum += response["count"]
        }
        return onlineNum
    }

    public async get_all_server_online_num() {
        let onlineNum: number = 0
        let server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        onlineNum += await this.getServerListOnlineNum(server_list)
        return onlineNum
    }

    public async is_in_server(account: string) {
        let resultServerConfig: ServerConfig = null
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            let response = await SocketClientReqManager.instance.req_server_is_in_server(account, server)
            if (response && response["isIn"]) {
                let isIn: boolean = response["isIn"]
                if (isIn) {
                    resultServerConfig = server;
                    return resultServerConfig;
                }
            }
        }
        return resultServerConfig
    }
    

}